package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.me

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.helasasa.maxfunds.lineofcredit.cashloan.ProjectApplication
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.IApiProcessCameroonCallback
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.createApiActionCameroon
import com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.http.doPostRequestCameroon
import org.json.JSONArray
import org.json.JSONObject

class MeCameroonViewModel : ViewModel() {

    private val _loadingResult = MutableLiveData<Boolean>()
    val loadingResult: LiveData<Boolean> = _loadingResult

    private val _pageDetailResult = MutableLiveData<MeCameroonResult>()
    val pageDetailResult: LiveData<MeCameroonResult> = _pageDetailResult

    /** 获取页面详情接口 */
    fun pageDetail() {
        _loadingResult.value = true

        val context = ProjectApplication.context
        val action = hashMapOf<String, Any>().createApiActionCameroon()
        action["path"] = "Jdvrwau2zkjCCvwpptC8w"

        context.doPostRequestCameroon(action, object : IApiProcessCameroonCallback {
            override fun onApiReady() {
            }

            override fun onApiDone(jsonObject: JSONObject) {
                _loadingResult.value = false

                val obj = jsonObject.optJSONObject("obj") ?: JSONObject()
                val customize = obj.optJSONObject("customize")
                if (customize != null) {
                    // 更新客服
                    val mobile = customize.optString("mobile")
                    val email = customize.optString("email")
                    ProjectApplication.updateHotlines(mobiles = mobile, emails = email)

                    // 更新首页额度
                    var homeAmount = customize.optString("homeAmount")
                    if (homeAmount.isBlank()) {
                        homeAmount = "300000"
                        customize.put("homeAmount", homeAmount)
                    }
                    ProjectApplication.updateHomeAmount(homeAmount)

                    // 更新权限
                    val permissions = customize.optJSONObject("permissions") ?: JSONObject()
                    val splashPer = permissions.optJSONArray("permission") ?: JSONArray()
                    val applyPer = permissions.optJSONArray("home") ?: JSONArray()
                    ProjectApplication.updateSplashPermissions(splashPer)
                    ProjectApplication.updateApplyPermissions(applyPer)

                    // 更新运营商列表-法语
                    val walletChannels = customize.optJSONArray("walletChannels") ?: JSONArray()
                    ProjectApplication.updateWalletChannels(walletChannels)

                    // 更新测试账号列表-法语
                    val accList = customize.optJSONArray("accListV2") ?: JSONArray()
                    ProjectApplication.updateAccUsers(accList)

                    _pageDetailResult.value =
                        MeCameroonResult(success = ProjectApplication.mHotlines)
                } else {
                    _pageDetailResult.value = MeCameroonResult(error = "Sys config error")
                }
            }

            override fun onApiError(code: String, errMsg: String) {
                _loadingResult.value = false

                _pageDetailResult.value =
                    MeCameroonResult(error = errMsg)
            }
        })

    }
}